function setCookie(cookieName,cvalue,expiredays,path)
{
	var expireDate=new Date();
	var expireStr="";
	if(expiredays!=null) {
		expireDate.setTime(expireDate.getTime()+(expiredays*24*3600*1000));
		expireStr="; expires="+expireDate.toGMTString();
	}
	pathStr=(path==null)?"; path=/":"; path="+path;
	document.cookie=cookieName+'='+escape(cvalue)+expireStr+pathStr;
}

function getCookie(cookieName)
{
	var index=-1;
 	if(document.cookie) 
 		index=document.cookie.indexOf(cookieName);
 	if(index==-1) {
 		return "";
 	} else {
 	     var iBegin = (document.cookie.indexOf("=", index) +1);
          var iEnd =document.cookie.indexOf(";", index);
          if (iEnd == -1)
          {
              iEnd = document.cookie.length;
          }
          return unescape(document.cookie.substring(iBegin,iEnd));
	}
}

function GetQueryString(param) {
	var request = {
		QueryString : function(val) {
			var uri = window.location.search;
			var re = new RegExp("" + val + "=([^&?]*)", "ig");
			return ((uri.match(re)) ? (decodeURI(uri.match(re)[0]
					.substr(val.length + 1))) : '');
		}
	}
	return request.QueryString(param);
}
var rtmp_publish_url = "";
var rtmp_play_url = "";
var hls_play_url = "";
var lssapp_id = GetQueryString("lssapp_id");
var stream_stu = GetQueryString("stream_stu");
var stu_selmic = 0;
var stu_selcam = 0;

function publishme() {
	lss.initConnect();
	var publishConf = {
		micID : stu_selmic,
		camID : stu_selcam,
		audioKBitrate : 44,
		audioSamplerate : 44100,
		videoFPS : 10,
		keyFrameInterval : 30,
		videoKBitrate : 256,
		videoQuality : 80,
		volume : 80,
		isUseCam : true,
		isUseMic : true,
		isMute : false
	};
	lss.startPublish(publishConf);
}

function closeme() {
	if (lss)
		lss.closeConnect();
}

function muteme(){
	if (lss)
		lss.setMute(true);
}

function nomuteme(){
	if (lss)
		lss.setMute(false);
}

function setStumic(selmic){
	stu_selmic = selmic;
}

function getStumic(){
	return stu_selmic;
}

function setStucam(selcam){
	stu_selcam = selcam;
}

function getStucam(){
	return stu_selcam;
}

function getMics(){
	if(lss) 
		return lss.getMic();
}

function getCams(){
	if(lss)
		return lss.getCam();
}

function genK(option) {
	
	$.ajax({
		type : "POST",
		timeout : 8000,
		url : "./remoteinvoke.php",
		dataType : "json",
		data : {
			method : 'genK',
			appid : option.appid,
			stream : option.stream
		},
		success : function(data) {
			if (data.Flag == 100) {
				if (option.success)
					option.success(data);
			} else {
				if (option.failure)
					option.failure(data.FlagString);
			}
		},
		error : option.failure
	});
}

var lss;

function initLss(){
	var stream_stu_tmp = stream_stu.replace(/-/g,'_');
	genK({
		appid : lssapp_id,
		stream : stream_stu_tmp,
		success : function(data) {
			console.log(data);
			
			lss = new aodianLss({
				container : 'stuPublish',
				url : rtmp_publish_url + '/' + lssapp_id + '/' + stream_stu_tmp + '?k=' + data.k + '&t=' + data.t ,
				width : '278',
				height : '180',
				autoconnect : true
			});
		},
		failure : function(errStr) {
			alert(errStr.responseText);
		}
	});
}

function initParam(){
	$.ajax({
		type : "POST",
		timeout : 8000,
		url : "./remoteinvoke.php",
		dataType : "json",
		data : {
			method : 's_url'
		},
		success : function(data) {
			if (data.Flag == 100) {
				rtmp_publish_url = data.rtmp_publish_url;
				rtmp_play_url = data.rtmp_play_url;
				hls_play_url = data.hls_play_url;
				initLss();
			} else {
				return;
			}
		},
		error : function(error) {
			return;
		}
	});
	
	var stu_selmic_cookie = getCookie("stu_selmic");
	if(stu_selmic_cookie) stu_selmic = stu_selmic_cookie;
	var stu_selcam_cookie = getCookie("stu_selcam");
	if(stu_selcam_cookie) stu_selcam = stu_selcam_cookie;
	
}

var playloop = 0;
function playsound(){
	if($.browser.msie) {
	    $('body').append('<bgsound src="sound/prompt.mp3" autostart=true loop=3/>');
	} else {
		playloop = 0;
		var audio = document.createElement("audio");  
	    audio.src = "sound/prompt.mp3";  
	    audio.addEventListener('ended', function () {  
	    	playloop++;
	        if(playloop>=3) 
	        	audio.pause();
	        else 
	        	audio.play();
	     }, false);  
	    audio.play();  
	}
}

$(function() {
	initParam();
	playsound();
});

